package com.corner.ui.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import com.corner.R;
import com.corner.model.cornerservice.domain.Location;
import com.corner.presenter.imp.LocationListPresenter;
import com.corner.ui.activity.ChannelActivity;
import com.corner.ui.activity.MainActivity;
import com.corner.ui.activity.SearchLocationActivity;
import com.corner.ui.adapter.LocationListAdapter;
import com.corner.ui.base.BaseFragment;
import com.corner.ui.view.ILocationListView;
import com.corner.utils.view.FindView;
import com.corner.utils.view.widget.XListView;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ObjectAnimator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/7/23，09.00.
 * 描述：
 */
public class LocationListFragment extends BaseFragment<LocationListPresenter> implements ILocationListView, View.OnClickListener, AdapterView.OnItemClickListener {

    public static final int SEARCH_LOCATION = 18;
    @FindView(R.id.lv_location_list)
    public XListView lvLocations;
    private View header;
    private LocationListAdapter adapter;
    private List<Location> locations;

    @Override
    protected LocationListPresenter createP() {
        return new LocationListPresenter();
    }

    @Override
    protected void setOnclick() {
        initHeader();
        lvLocations.addHeaderView(header);
        header.findViewById(R.id.et_select_location_search).setOnClickListener(this);
        lvLocations.setAutoLoadEnable(true);
        lvLocations.setPullLoadEnable(false);
        lvLocations.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                presenter.refressh();
            }

            @Override
            public void onLoadMore() {
                presenter.loadMore();
            }
        });
        adapter = new LocationListAdapter(activity, new ArrayList<Location>());
        lvLocations.setAdapter(adapter);
        lvLocations.autoRefresh();
        lvLocations.setOnItemClickListener(this);
    }

    /**
     * 初始化头布局
     */
    private void initHeader() {
        header = View.inflate(activity, R.layout.view_location_list_header, null);
    }

    @Override
    protected View initView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.frag_location_list, null);
    }

    @Override
    public void showRefreshLocationList(List<Location> locations) {
        if (adapter == null) {
            adapter = new LocationListAdapter(activity, this.locations);
        } else {
            adapter.changeList(locations);
        }
        this.locations = locations;
    }

    @Override
    public void showLoadMoreLocationList(List<Location> locations) {
        if (adapter == null) {
            adapter = new LocationListAdapter(activity, locations);
        } else {
            adapter.changeList(locations);
        }
        this.locations = locations;
    }

    @Override
    public void refreshComplete() {
        lvLocations.stopRefresh();
    }

    @Override
    public void loadMoreComplete() {
        lvLocations.stopLoadMore();
    }

    @Override
    public void setLoadMoreEnable(boolean b) {
        lvLocations.setPullLoadEnable(b);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.et_select_location_search:
                ObjectAnimator animator = ObjectAnimator.ofFloat(((MainActivity) activity).llRoot, "translationY", -((MainActivity) activity).actionBar.viewActionBar.getHeight());
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        Intent intent = new Intent(activity, SearchLocationActivity.class);
                        intent.putExtra(SearchLocationActivity.INITENT_ID, LocationListFragment.SEARCH_LOCATION);
                        startActivityForResult(intent, LocationListFragment.SEARCH_LOCATION);
                        activity.overridePendingTransition(0, 0);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }
                });

                animator.setDuration(300);
                animator.start();
                break;
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (id >= 0) {
            Intent intent = new Intent(activity, ChannelActivity.class);
            intent.putExtra(ChannelActivity.LOCATION_ID, locations.get((int) id).getObjectId());
            intent.putExtra(ChannelActivity.LOCATION_NAME, locations.get((int) id).getLocationName());
            startActivity(intent);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        activity.overridePendingTransition(0, 0);
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == LocationListFragment.SEARCH_LOCATION) {
            View viewActionBar = ((MainActivity) activity).actionBar.viewActionBar;
            ObjectAnimator animator = ObjectAnimator.ofFloat(((MainActivity) activity).llRoot, "translationY", viewActionBar.getTop() - viewActionBar.getHeight(), viewActionBar.getTop());
            animator.setDuration(300);
            animator.start();
        }
    }

    public void upToRefresh() {
        lvLocations.setSelection(0);
        lvLocations.autoRefresh();
    }
}
